﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="erakonnanimekiri_vastus", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(ErakonnanimekiriVastus))]
    public class ErakonnanimekiriVastusSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IErakonnanimekiriVastus clazz;
        
        public ErakonnanimekiriVastusSerializer(IErakonnanimekiriVastus clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("kood");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "integer");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdInteger(((decimal)(clazz.Kood))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("nimi");
            if (clazz.Nimi!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Nimi))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Nimi");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("esit_kpv");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "date");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdDate(((System.DateTime)(clazz.EsitKpv))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("liikmed");
            if (clazz.Liikmed!=null) {
                Writer.WriteTypeAttribute("http://schemas.xmlsoap.org/soap/encoding/", "Array");
                Writer.WriteArrayTypeAttribute("erakonnanimekiri_liige", "http://producers.arireg.xtee.riik.ee/producer/arireg", clazz.Liikmed.Count);
                for (int x = 0; ((clazz.Liikmed != null) 
                            && (x < clazz.Liikmed.Count)); x = (x + 1)) {
                    if (clazz.Liikmed[x]!=null) {
                        Writer.WriteStartElement("item");
                        global::Xtee.Core.SerializationMetadata metaLiikmed = this.GetMetaData(clazz.Liikmed[x]);
                        Writer.WriteTypeAttribute(metaLiikmed.TypeQualifiedName.Namespace, metaLiikmed.TypeQualifiedName.Name);
                        metaLiikmed.NewSerializer(clazz.Liikmed[x]).WriteXml(Writer);
                        Writer.WriteEndElement();
                    }
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Liikmed");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="kood") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>erakonnanimekiri_vastus>kood");
                }
                clazz.Kood = new global::Xtee.Core.Schema.XsdInteger(Reader.ReadElementString("kood")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>erakonnanimekiri_vastus>kood", Reader.LocalName);
            }
            if (Reader.LocalName=="nimi") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>erakonnanimekiri_vastus>nimi");
                }
                clazz.Nimi = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("nimi")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>erakonnanimekiri_vastus>nimi", Reader.LocalName);
            }
            if (Reader.LocalName=="esit_kpv") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>erakonnanimekiri_vastus>esit_kpv");
                }
                clazz.EsitKpv = new global::Xtee.Core.Schema.XsdDate(Reader.ReadElementString("esit_kpv")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>erakonnanimekiri_vastus>esit_kpv", Reader.LocalName);
            }
            if (Reader.LocalName=="liikmed") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>erakonnanimekiri_vastus>liikmed");
                }
                clazz.Liikmed = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IErakonnanimekiriLiige>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("liikmed");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.IsElementNil()) {
                            throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>erakonnanimekiri_liikmed>item");
                        }
                        global::Xtee.Core.SerializationMetadata metaLiikmed = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IErakonnanimekiriLiige>(Reader, "erakonnanimekiri_liige", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                        global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IErakonnanimekiriLiige item = metaLiikmed.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IErakonnanimekiriLiige>();
                        clazz.Liikmed.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            Reader.ReadStartElement("item");
                            metaLiikmed.NewSerializer(item).ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>erakonnanimekiri_vastus>liikmed", Reader.LocalName);
            }
        }
    }
}
